package com.yhos.platform.oopston.core.web;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.condition.RequestCondition;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 对于不同渠道类型执行不同的Controller
 */
@Component
@Scope("prototype")
public class ChannelTypeRequestCondition implements RequestCondition<ChannelTypeRequestCondition> {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private String[] channels;

    public ChannelTypeRequestCondition(String[] channels) {
        logger.info("---ChannelTypeRequestCondition.ChannelTypeRequestCondition---");
        this.channels = channels;
    }

    @Override
    public ChannelTypeRequestCondition combine(ChannelTypeRequestCondition other) {
        logger.info("---ChannelTypeRequestCondition.combine---");
        String[] allChannels = this.mergeChannels(other.channels);
        logger.info("---ChannelTypeRequestCondition.combine.allChannels---: " + allChannels);
        return new ChannelTypeRequestCondition(allChannels);
    }

    @Override
    public ChannelTypeRequestCondition getMatchingCondition(HttpServletRequest request) {
        logger.info("---ChannelTypeRequestCondition.getMatchingCondition---");
        String channel = this.getChannelByParameter(request.getAttribute("channelType") != null ? (String) request.getAttribute("channelType") : "");
        for (int i = 0; i < channels.length; i++) {
            if (channel.equalsIgnoreCase(channels[i])) {
                return this;
            }
        }
        return null;
    }

    @Override
    public int compareTo(ChannelTypeRequestCondition other, HttpServletRequest httpServletRequest) {
        logger.info("---ChannelTypeRequestCondition.compareTo---");
        return null != channels && null == other.channels ? 1
                : null == channels && null != other.channels ? -1 : 0;
    }

    private String getChannelByParameter(String parameter) {
        logger.info("---ChannelTypeRequestCondition.getChannelByParameter---");
//        if (parameter.equalsIgnoreCase("1") || parameter.equalsIgnoreCase("2")
//            || parameter.equalsIgnoreCase("3") || parameter.equalsIgnoreCase("4")) {
//            return "OST";
//        } else
            if (parameter.equalsIgnoreCase("5") || parameter.equalsIgnoreCase("6")
                || parameter.equalsIgnoreCase("7") || parameter.equalsIgnoreCase("8")) {
            return "ZT";
        }

        return "";
    }

    private String[] mergeChannels(String[] otherChannels) {
        logger.info("---ChannelTypeRequestCondition.mergeChannels---");
        if (null == otherChannels) {
            return channels;
        }

        String[] results = new String[channels.length + otherChannels.length];
        for (int i = 0; i < channels.length; i++) {
            results[i] = channels[i];
        }

        for (int i = channels.length; i < results.length; i++) {
            results[i] = otherChannels[i - channels.length];
        }

        return results;
    }

    private String getPostBody(HttpServletRequest request) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String line = null;
        StringBuilder sb = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
